<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use App\Models\VillagePublish;
use App\Models\VillagePublishCheck;
use App\Services\SensitiveWords;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class VillagePublishController extends Controller
{

    /*
     * 列表
     * @param $request
     * @return array
     */
    public function villagePublishList(Request $request)
    {
        $pagesize  = $request -> input('pagesize');
        $region_id = $request -> input('region_id');


        $villagePublish = VillagePublish::village_pub_list($region_id,$pagesize);
        return jsonSuccess($villagePublish);

    }


    /*
    * 操作表
    * @param $request
    * @return bool
    */
    public function villagePubOperated(Request $request)
    {
        $villagePubId = $request -> input('village_pub_id');
        $type         = $request -> input('type');
        if(!$villagePubId || !is_numeric($villagePubId)){
            return jsonError(4020);
        }

        if(!$type){
            return jsonError(4020);
        }


        $operated = VillagePublish::village_pub_operated($villagePubId,$type);

        if($operated){
            return jsonSuccess();
        }else{
            return jsonError();
        }

    }


    /*
    * 详情页
    * @param $request
    * @return bool
    */
    public function details(Request $request)
    {
        $villagePubId = $request -> input('village_pub_id');


        if(!$villagePubId || !is_numeric($villagePubId)){
            return jsonSuccess();
        }

        // 阅读数自增
        VillagePublish::village_pub_operated($villagePubId,'1');
        $VillagePublish = VillagePublish::with(['village_pub_admin_author:id,name,avatar']) -> find($villagePubId);


        return jsonSuccess($VillagePublish);
    }

    /*
     * 农品助力发布
     * @param Request $request
     * @return string
     */
    public function villagePublish(Request $request)
    {

        $message = [
            'pub_title.required' => '请输入标题',
            'pub_content.required' => '请输入内容',
            'contact.required' => '请输入联系人',
            'mobile.required' => '请输入联系电话',
            'mobile.regex' => '请输入正确的手机号'
        ];

        $validator = Validator::make($request ->all(),[
            'pub_title' => 'required',
            'pub_content' => 'required',
            'contact' => 'required',
            'mobile' => ['required','regex:/^[1](([3][0-9])|([4][5-9])|([5][0-3,5-9])|([6][5,6])|([7][0-8])|([8][0-9])|([9][1,8,9]))[0-9]{8}$/'],
        ],$message);

        if($validator -> fails()){
            return jsonError($validator -> messages() -> first());
        }
        $param = $request -> all();

        //用户角色验证
        $adminUser = User::isAdminVillageUser();
        if(!$adminUser){
            return jsonError('', 4044, 401);
        }
        $param['region_id'] = $adminUser->region_id;
        $param['admin_id'] = $adminUser->id;
        $villagePublish = VillagePublish::store($param);
        if($villagePublish){

            return jsonSuccess(2007);
        }else{
            return jsonError();
        }
    }



    /*
   * 审核
   * @param $request
   * @return bool
   */
    public function villagePubCheck(Request $request)
    {


        $param = [
            'check_user_id'   => $request -> input('user_id'),
            'pub_id'          => $request -> input('village_pub_id'),
            'is_admin'        => $request -> input('is_admin') ?? 0,
            'refuse_reason'   => $request -> input('refuse_reason')
        ];

        if(!$param['check_user_id'] || !$param['pub_id']){
            return jsonError(4020);
        }

        if(!is_numeric($param['check_user_id']) || !is_numeric($param['pub_id'])){
            return jsonError(4020);
        }


        if(!VillagePublish::find($param['pub_id'])){
            return jsonError('未找到此文章',4000,404);
        }


        $villagePubCheck = VillagePublishCheck::store($param);

        if($villagePubCheck){
            return jsonSuccess(2007);
        }else{
            return jsonError();
        }
    }

}
